package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Recharge;
import cn.wolfcode.crm.mapper.MemberMapper;
import cn.wolfcode.crm.mapper.RechargeMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IRechargeService;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class RechargeServiceImpl implements IRechargeService {
    @Autowired
    private RechargeMapper rechargeMapper;
    @Autowired
    private MemberMapper memberMapper;

    @Override
    public PageResult list(QueryObject qo) {
        int total = rechargeMapper.queryForCount(qo);
        if(total==0){
            return new PageResult();
        }
        List<Recharge> rows = rechargeMapper.queryForList(qo);
        return new PageResult(total,rows);
    }

    //充值:需要更新会员表的赠送金额和余额,赠送金额累加,余额在余额的基础上累加本次充值金额
    public void rechargeMoney(Recharge recharge) {
        //本次充值金额,赠送金额,账户增加的总金额
        BigDecimal rechargeMoney = recharge.getRechargeMoney();
        BigDecimal giftMoney = recharge.getGiftMoney();
        BigDecimal totalMoney = rechargeMoney.add(giftMoney);
        BigDecimal oldAccount=recharge.getOldAccount();
        BigDecimal newAccount = oldAccount.add(totalMoney);
        //更新会员表中的赠送金额和总金额
        memberMapper.updateGiftAndInitAccount(recharge.getMember().getId(),giftMoney,rechargeMoney);
        //充值时间
        recharge.setRechargeTime(new Date());
        //充值后余额
        recharge.setNewAccount(newAccount);
        //保存充值信息
        rechargeMapper.insert(recharge);
    }

    @Override
    public void unrechargeMoney(Recharge recharge) {

        BigDecimal rechargeMoney = BigDecimal.ZERO.subtract(recharge.getRechargeMoney());//扣费金额
        recharge.setRechargeMoney(rechargeMoney);
        BigDecimal giftMoney = recharge.getGiftMoney()!=null?recharge.getGiftMoney():BigDecimal.ZERO;
        BigDecimal totalMoney = rechargeMoney.add(giftMoney);
        BigDecimal oldAccount=recharge.getOldAccount();
        BigDecimal newAccount = oldAccount.add(totalMoney);
        memberMapper.updateGiftAndInitAccount(recharge.getMember().getId(),giftMoney,rechargeMoney);
        recharge.setRechargeTime(new Date());
        recharge.setNewAccount(newAccount);
        rechargeMapper.insert(recharge);
    }

    @Override
    public void returnMoney(Recharge recharge) {
        BigDecimal rechargeMoney = BigDecimal.ZERO.subtract(recharge.getRechargeMoney());//退还金额
        recharge.setRechargeMoney(rechargeMoney);
        BigDecimal giftMoney = recharge.getGiftMoney()!=null?recharge.getGiftMoney():BigDecimal.ZERO;
        BigDecimal totalMoney = rechargeMoney.add(giftMoney);
        BigDecimal oldAccount=recharge.getOldAccount();
        BigDecimal newAccount = oldAccount.add(totalMoney);
        memberMapper.updateGiftAndInitAccount(recharge.getMember().getId(),giftMoney,rechargeMoney);
        recharge.setRechargeTime(new Date());
        recharge.setNewAccount(newAccount);
        rechargeMapper.insert(recharge);

    }
}
